package com.gxuwz.controller;

import com.gxuwz.model.entity.Course;
import com.gxuwz.model.vo.Result;
import com.gxuwz.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/course")
@CrossOrigin
public class CourseController {

    @Autowired
    private CourseService courseService;

    // 显示课程列表
    @GetMapping("/list")
    public Result<List<Course>> listCourses() {
        List<Course> courses = courseService.getAllCourses();
        return Result.succeed(courses);
    }

    // 根据 courseID 查询课程信息
    @GetMapping("/courseID/{courseID}")
    public Result<Course> getCourseByCourseID(@PathVariable String courseID) {
        Course course = courseService.getCourseByCourseID(courseID);
        return course != null ? Result.succeed(course) : Result.error("未找到该课程");
    }

    // 根据 courseName 查询课程信息
    @GetMapping("/courseName/{courseName}")
    public Result<Course> getCourseByCourseName(@PathVariable String courseName) {
        Course course = courseService.getCourseByCourseName(courseName);
        return course != null ? Result.succeed(course) : Result.error("未找到该课程");
    }

    // 添加课程
    @PostMapping("/add")
    public Result addCourse(@RequestBody Course course) {
        boolean result = courseService.addCourse(course);
        return result ? Result.succeed("课程添加成功") : Result.error("课程添加失败");
    }

    // 更新课程信息
    @PutMapping("/update")
    public Result updateCourse(@RequestBody Course course) {
        System.out.println("course:" + course);
        boolean result = courseService.updateCourse(course);
        return result ? Result.succeed("课程信息更新成功") : Result.error("课程信息更新失败");
    }

    // 删除课程
    @DeleteMapping("/delete/{courseID}")
    public Result deleteCourse(@PathVariable String courseID) {
        boolean isDeleted = courseService.deleteCourseByCourseID(courseID);
        return isDeleted ? Result.succeed("课程删除成功") : Result.error("课程删除失败");
    }
}
